package cn.lf.android.lawfirm.outgoingregistration;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import cn.lf.android.lawfirm.BaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.outgoingregistration.controller.AddOutGoingConroller;
import cn.lf.android.lawfirm.outgoingregistration.controller.OutGoingInfoConroller;
import cn.lf.android.lawfirm.outgoingregistration.modle.OutGoingInfo;
import cn.lf.android.lawfirm.utils.timepicker.TimePicker;

import static cn.lf.android.lawfirm.R.id.content_address;
import static cn.lf.android.lawfirm.R.id.et_matters;
import static cn.lf.android.lawfirm.R.id.tv_time;

/**
 * 外出登记添加
 * Created by Administrator on 2017/8/22.
 */

public class AddOutGoingActivity extends BaseActivity implements AddOutGoingConroller.AddOutGoingCallback, OutGoingInfoConroller.OutGoingInfoCallback {
    /**
     * 0代表添加，1代表编辑
     **/
    private String type;
    /**
     * 外出日期部分
     **/
    private AutoRelativeLayout mRlData;
    /**
     * 外出日期
     **/
    private TextView mData;
    /**
     * 起始时间部分
     **/
    private AutoRelativeLayout mRlBeginTime;
    /**
     * 起始时间
     **/
    private TextView mBeginTime;
    /**
     * 结束时间部分
     **/
    private AutoRelativeLayout mRlEndTime;
    /**
     * 结束时间
     **/
    private TextView mEndTime;
    /**
     * 外出地点部分
     **/
    private AutoRelativeLayout mRlAddress;
    /**
     * 外出地点
     **/
    private EditText mAddress;
    /**
     * 外出事项
     **/
    private EditText mMatters;
    private ImageView mLeft;
    private TextView mTitle;
    private TextView mCommit;
    AddOutGoingConroller mAddOutGoingConroller;
    private TimePicker mTimePicker_start;
    private TimePicker mTimePicker_end;
    private String id = "0";
    private OutGoingInfoConroller mOutGoingInfoConroller;

    @Override
    public Object getContentResView() {
        return R.layout.activity_addoutgoing;
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        initTitle();
        mRlData = ((AutoRelativeLayout) findViewById(R.id.rl_data));
        mData = ((TextView) findViewById(R.id.content_data));
        mRlBeginTime = ((AutoRelativeLayout) findViewById(R.id.rl_begintime));
        mBeginTime = ((TextView) findViewById(R.id.content_begintime));
        mRlEndTime = ((AutoRelativeLayout) findViewById(R.id.rl_endtime));
        mEndTime = ((TextView) findViewById(R.id.content_endtime));
        mRlAddress = ((AutoRelativeLayout) findViewById(R.id.rl_address));
        mAddress = ((EditText) findViewById(content_address));
        mMatters = ((EditText) findViewById(et_matters));
        mCommit = ((TextView) findViewById(R.id.tv_commit));
        if ("外出登记编辑".equals(type)) {
            id = intent.getStringExtra("id");
            mOutGoingInfoConroller = new OutGoingInfoConroller(this, this);
            mOutGoingInfoConroller.onOutGoingInfo(id);
        } else if ("添加外出登记".equals(type)) {
            id = "0";
        }
    }

    @Override
    public void initData() {
        mCommit.setOnClickListener(this);
        mRlData.setOnClickListener(this);
        mRlBeginTime.setOnClickListener(this);
        mRlEndTime.setOnClickListener(this);
        mRlAddress.setOnClickListener(this);
        mAddOutGoingConroller = new AddOutGoingConroller(this, this);
        mTimePicker_end = new TimePicker(this, 5, false,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        mEndTime.setText(timeString);
                    }
                });
        mTimePicker_start = new TimePicker(this, 5, false,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        mBeginTime.setText(timeString);
                    }
                });
    }

    private void initTitle() {
        mLeft = ((ImageView) findViewById(R.id.img_left));
        mLeft.setVisibility(View.VISIBLE);
        mLeft.setOnClickListener(this);
        mTitle = ((TextView) findViewById(R.id.eb_tv_title));
        mTitle.setText("外出登记");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_left:
                hintKbTwo();
                finish();
                break;
            case R.id.tv_commit://提交
                hintKbTwo();
                if (check()) {
                    mAddOutGoingConroller.onAddOutGoing(id, mBeginTime.getText().toString(), mEndTime.getText().toString(), mAddress.getText().toString().trim(), mMatters.getText().toString().trim());
                }
                break;
            case R.id.rl_data://外出日期
                hintKbTwo();
                break;
            case R.id.rl_begintime://起始時間
                hintKbTwo();
                mTimePicker_start.show(getWindow().getDecorView());
                break;
            case R.id.rl_endtime://结束时间
                hintKbTwo();
                mTimePicker_end.show(getWindow().getDecorView());
                break;
            case R.id.rl_address://外出地点
                hintKbTwo();
                break;
        }
    }

    public boolean check() {
        if (mBeginTime.getText().toString().length() == 0) {
            showCToast("请选择开始时间");
            return false;
        }
        if (mEndTime.getText().toString().length() == 0) {
            showCToast("请选择结束时间");
            return false;
        }
        if (mAddress.getText().toString().length() == 0) {
            showCToast("请输入外出地点");
            return false;
        }
        if (mMatters.getText().toString().length() == 0) {
            showCToast("请输入外出事项");
            return false;
        }
        return true;
    }

    @Override
    public void onAddOutGoingSuccessed(String str) {
        OutGoingRegistrationActivity.isRefresh=true;
        showCToast("操作成功");
        finish();
    }

    @Override
    public void onAddOutGoingFailed() {
        showCToast("操作失败");
    }

    OutGoingInfo mBean;

    @Override
    public void onOutGoingInfouccessed(OutGoingInfo beans) {
        mBean = beans;
        mBeginTime.setText(beans.startTime);
        mEndTime.setText(beans.endTime);
        mAddress.setText(beans.destination);
        mMatters.setText(beans.reason);
    }

    @Override
    public void onOutGoingInfoFailed() {

    }
}
